<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增班级 </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
        <template v-if="column.key === 'clazzName'">
          <div style="width: 100%;cursor: pointer;" @click="openUserModal(record)">{{record.clazzName}}</div>
        </template>
      </template>
    </BasicTable>
    <FormModal @register="register" @success="handleSuccess" />
    <UsersModal @register="userModal[0]" @success="handleSuccess" />

  </div>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { listApi,deleteApi,getLevelsApi } from '@/api/sys/clazz';
  import {ref} from 'vue';

  import FormModal from './FormModal.vue';
  import UsersModal from './UsersModal.vue';

  import { columns, searchFormSchema } from './role.data';
  import {useMessage} from "@/hooks/web/useMessage";
  import {useModal} from "@/components/Modal";

  defineOptions({ name: 'clazzList' });
  const { createMessage } = useMessage();

  const [register, { openModal, closeModal }] = useModal();
  const userModal = useModal();


  const [registerTable, { reload }] = useTable({
    title: '班级列表',
    api: listApi,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    actionColumn: {
      width: 80,
      title: '操作',
      dataIndex: 'action',
      // slots: { customRender: 'action' },
      fixed: undefined,
    },
  });

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  function handleDelete(record: Recordable) {
    //console.log(record);

    deleteApi(record).then(res => {
      createMessage.success(res.message);
      })
      .catch((res) => {
        createMessage.error(res.message);
      })
      .finally(() => {
        reload();
      });
  }

  function handleSuccess() {
    reload();
  }

  function openUserModal(record: Recordable){
  //  console.log("--record::",record);
    let json={
      clazzId:record.clazzId
    };
    getLevelsApi(json).then(res=>{
      if(res.code==200){
        // console.log("---res.data")
        // console.log(res.data);
        let arr=[];
        res.data.forEach(r=>{
          arr.push(r.levelName);
        });
        userModal[1].openModal(true, {
          levelNames:arr.join(",")
        });
      }else{
        createMessage.error(res.message);
      }
    })

  }

</script>
